package com.edu.exam.entity.curriculum.questionbank;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import jakarta.validation.constraints.DecimalMin;
import jakarta.validation.constraints.Min;
import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 论述题实体类
 * 所属模块：B-02 题库管理模块
 * 对应数据库表：essay_question_base
 *
 * 管理论述题的基础信息，包括题目内容、背景材料、答题要求、分值等。
 * 支持论述题的创建、编辑、审核、发布等全生命周期管理。
 * 关联参考答案、评分标准、质量评估等信息。
 *
 * @author System
 * @version 1.0.0
 */
@Entity
@Comment("论述题基础信息表")
@SQLDelete(sql = "UPDATE essay_question_base SET is_deleted = true, delete_time = CURRENT_TIMESTAMP WHERE id = ?")
@Where(clause = "is_deleted = false")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class EssayQuestion {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotBlank(message = "试题编号不能为空")
    @Size(max = 50, message = "试题编号长度不能超过50个字符")
    @Column(name = "question_code", nullable = false, length = 50, unique = true)
    @Comment("试题编号")
    private String questionCode;

    @NotBlank(message = "题目标题不能为空")
    @Size(max = 200, message = "题目标题长度不能超过200个字符")
    @Column(name = "title", nullable = false, length = 200)
    @Comment("题目标题")
    private String title;

    @NotBlank(message = "试题内容不能为空")
    @Column(name = "content", nullable = false, columnDefinition = "TEXT")
    @Comment("试题内容")
    private String content;

    @Column(name = "background_material", columnDefinition = "TEXT")
    @Comment("背景材料")
    private String backgroundMaterial;

    @Column(name = "requirements", columnDefinition = "TEXT")
    @Comment("答题要求")
    private String requirements;

    @Min(value = 0, message = "字数限制不能为负数")
    @Column(name = "word_limit")
    @Comment("字数限制")
    private Integer wordLimit;

    @Min(value = 0, message = "时间限制不能为负数")
    @Column(name = "time_limit")
    @Comment("时间限制(分钟)")
    private Integer timeLimit;

    @NotNull(message = "分值不能为空")
    @DecimalMin(value = "0.0", message = "分值不能为负数")
    @Column(name = "score", nullable = false, precision = 10, scale = 2)
    @Comment("分值")
    private BigDecimal score;

    @NotBlank(message = "难度等级不能为空")
    @Column(name = "difficulty_level", nullable = false, length = 20)
    @Comment("难度等级")
    private String difficultyLevel;

    @NotBlank(message = "题目状态不能为空")
    @Column(name = "status", nullable = false, length = 20)
    @Comment("题目状态")
    private String status;

    @NotNull(message = "创建人ID不能为空")
    @Column(name = "creator_id", nullable = false)
    @Comment("创建人ID")
    private Long creatorId;

    @NotBlank(message = "创建人姓名不能为空")
    @Size(max = 100, message = "创建人姓名长度不能超过100个字符")
    @Column(name = "creator_name", nullable = false, length = 100)
    @Comment("创建人姓名")
    private String creatorName;

    @Column(name = "reviewer_id")
    @Comment("审核人ID")
    private Long reviewerId;

    @Size(max = 100, message = "审核人姓名长度不能超过100个字符")
    @Column(name = "reviewer_name", length = 100)
    @Comment("审核人姓名")
    private String reviewerName;

    @NotNull(message = "创建时间不能为空")
    @Column(name = "created_time", nullable = false)
    @Comment("创建时间")
    private LocalDateTime createdTime;

    @NotNull(message = "更新时间不能为空")
    @Column(name = "updated_time", nullable = false)
    @Comment("更新时间")
    private LocalDateTime updatedTime;

    @Column(name = "review_time")
    @Comment("审核时间")
    private LocalDateTime reviewTime;

    @Column(name = "publish_time")
    @Comment("发布时间")
    private LocalDateTime publishTime;

    @NotBlank(message = "版本号不能为空")
    @Size(max = 20, message = "版本号长度不能超过20个字符")
    @Column(name = "version", nullable = false, length = 20)
    @Comment("版本号")
    @Builder.Default
    private String version = "1.0";

    @Size(max = 20, message = "上一版本号长度不能超过20个字符")
    @Column(name = "previous_version", length = 20)
    @Comment("上一版本号")
    private String previousVersion;

    @NotNull(message = "删除标志不能为空")
    @Column(name = "is_deleted", nullable = false)
    @Comment("是否删除")
    @Builder.Default
    private Boolean isDeleted = false;

    @Column(name = "delete_time")
    @Comment("删除时间")
    private LocalDateTime deleteTime;

    @Size(max = 500, message = "删除原因长度不能超过500个字符")
    @Column(name = "delete_reason", length = 500)
    @Comment("删除原因")
    private String deleteReason;

    @NotNull(message = "题目分类ID不能为空")
    @Column(name = "category_id", nullable = false)
    @Comment("题目分类ID")
    private Long categoryId;

    @Column(name = "subject_id")
    @Comment("学科ID")
    private Long subjectId;

    @Column(name = "department_id")
    @Comment("院系ID")
    private Long departmentId;

    @Column(name = "tags", columnDefinition = "TEXT")
    @Comment("标签(JSON格式)")
    private String tags;

    @Column(name = "meta_data", columnDefinition = "TEXT")
    @Comment("扩展元数据(JSON格式)")
    private String metaData;

}
